package com.apex.sdk.utils

import android.content.Context
import android.graphics.Bitmap

import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream

object WatchFaceUtils {

    /**
     * 将asset文件写入缓存
     */
    fun writeTempCacheFile(context: Context, fileName: String?) {
        val cacheDir = context.cacheDir
        if (!cacheDir.exists()) {
            cacheDir.mkdirs()
        }
        val outFile = File(cacheDir, fileName)
        if (!outFile.exists()) {
            val res = outFile.createNewFile()
            if (!res) {
                return
            }
        } else {
            if (outFile.length() > 10) { //表示已经写入一次
                return
            }
        }
        outFile.writeBytes(context.resources.assets.open(fileName!!).readBytes())
    }

      fun createFileByDeleteOldFile(file: File?): Boolean {
        if (file == null) return false
        // file exists and unsuccessfully delete then return false
        if (file.exists() && !file.delete()) return false
        return if (!createOrExistsDir(file.parentFile)) false else try {
            file.createNewFile()
        } catch (e: IOException) {
            e.printStackTrace()
            false
        }
    }

    private fun createOrExistsDir(file: File?): Boolean {
        return file != null && if (file.exists()) file.isDirectory else file.mkdirs()
    }

   public fun getPicturePixel(bitmap: Bitmap): ByteArray {
        val width = bitmap.width
        val height = bitmap.height

        // 保存所有的像素的数组，图片宽×高
        val pixels = IntArray(width * height)
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height)
        val pixelsBytes = ArrayList<Byte>()
        for (clr in pixels) {
            val red = clr and 0x00ff0000 shr 16 // 取高两位
            val green = clr and 0x0000ff00 shr 8 // 取中两位
            val blue = clr and 0x000000ff // 取低两位
            val pixelsone = red and 0xF8 or (green shr 5)
            val pixelstwo = green and 0x1C shl 3 or (blue shr 3)
            pixelsBytes.add(pixelsone.toByte())
            pixelsBytes.add(pixelstwo.toByte())
        }
        val bytes = ByteArray(pixelsBytes.size + 4)
        val picHead = 4 or (width shl 10) or (height shl 21)
        val picHeadByte = intToMinByteArray(picHead)
        System.arraycopy(picHeadByte, 0, bytes, 0, picHeadByte.size)
        for (i in pixelsBytes.indices) {
            bytes[picHeadByte.size + i] = pixelsBytes[i]
        }
        val length = bytes.size
        val len = intToMinByteArray(length)
        val picbytes = ByteArray(len.size + bytes.size)
        System.arraycopy(len, 0, picbytes, 0, len.size)
        System.arraycopy(bytes, 0, picbytes, len.size, bytes.size)
        return picbytes
    }

    private fun intToMinByteArray(i: Int): ByteArray {
        val result = ByteArray(4)
        // 由高位到低位
        result[3] = (i shr 24 and 0xFF).toByte()
        result[2] = (i shr 16 and 0xFF).toByte()
        result[1] = (i shr 8 and 0xFF).toByte()
        result[0] = (i and 0xFF).toByte()
        return result
    }
}